<!--
* description:S-维修结算查询
* author:shensai
* createdDate: 2021-2-23
-->
<template>
  <div>
    <div class="app-container app-container-table">
      <one-table-template
        ref="multipleTable"
        :dynamic-buttons="tableButtons"
        :dynamic-components="tableComponents"
        :dynamic-api-config="apiConfig"
        :dynamic-table-cols="tableCols"
        :dynamic-form-fields="formField"
        :dynamic-is-init-table="true"
        :dynamic-is-show-select="false"
        :dynamic-is-show-more-btn="false"
        :callback-obj="callBackObj"
      />
    </div>
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import { exportExcel } from '@/utils/exportExcel'
import api from './api'
import Vue from 'vue'

export default {
  name: 'MaintenanceSettlement',
  components: {
    OneTableTemplate
  },

  mixins: [oneTableWithViewTemplateMixins],
  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData(
      [to.path, 'supplierMessage-edit'],
      () => {
        next()
      }
    )
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path
    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])
    return {
      titleName: '',
      editRowData: {},
      editPopupsState: '',
      editPopupsVisible: false,
      // 网格查询API配置对象
      apiConfig: api.queryDataUsingPOST_7,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'), // 查询
          name: 'search', // 按钮图标样式
          position: 'right'
        },
        {
          compKey: 'btnKey2',
          size: 'small',
          clickEvent: () => this.exportExcel(), // 导出
          text: '导出',
          name: 'export',
          position: 'left'
        },
        {
          compKey: 'btnKey3',
          size: 'small',
          name: 'reset',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          position: 'right' // 重置
        }
      ],
      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            // 显示组件
            {
              compKey: 'compKey1',
              labelName: '到店日期：',
              codeField: 'repairTimeStart,repairTimeEnd',
              component: () => import('@/components/org/datePicker/datePicker'),
              dateType: 'daterange',
              isMust: true,
              // isMul: true
            },
            {
              compKey: 'compKey2',
              labelName: '派工单号：',
              codeField: 'repairOrderCode',
              component: () => import('@/components/org/commonInput'),
              isInput: true,
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '车牌号：',
              codeField: 'carLicense',
              component: () => import('@/components/org/commonInput'),
              isInput: true,
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: 'VIN码：',
              codeField: 'vin',
              component: () => import('@/components/org/commonInput'),
              isInput: true,
              isMust: true
            },
            {
              compKey: 'compKey5',
              labelName: '送修人：',
              codeField: 'repairMan',
              component: () => import('@/components/org/commonInput'),
              isInput: true,
              isMust: false
            },
            {
              compKey: 'compKey6',
              labelName: '送修人电话：',
              codeField: 'repairTel',
              component: () => import('@/components/org/commonInput'),
              isInput: true,
              isMust: false
            },
            {
              compKey: 'compKey7',
              labelName: '车主姓名：',
              codeField: 'custName',
              component: () => import('@/components/org/commonInput'),
              isInput: true,
              isMust: false
            },
            {
              compKey: 'compKey8',
              labelName: '车主电话：',
              codeField: 'custTel',
              component: () => import('@/components/org/commonInput'),
              isInput: true,
              isMust: false
            },
            {
              compKey: 'compKey9',
              labelName: '服务顾问：',
              codeField: 'empCode',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'saEmpId,empName',
              customObj: {
                api: api.serviceConsultant
              },
              isMust: false
            }
            // {
            //   compKey: 'comKey10',
            //   labelName: '门店编码：',
            //   codeField: 'dlrCode',
            //   component: () => import('@/views/ve/planReport/components/multipleSearchInput'),
            //   type: 'inputText',
            //   dataToObject: {
            //     // 控制弹窗页面 搜索 网格 元素
            //     tableComponents: [
            //       {
            //         compKey: 'compKey1',
            //         labelName: '门店编码：',
            //         codeField: 'dlrCode',
            //         component: () => import('@/components/org/commonInput'),
            //         type: 'inputText',
            //         isMust: true,
            //         span: 8
            //       },
            //       {
            //         compKey: 'compKey2',
            //         labelName: '门店名称：',
            //         codeField: 'dlrFullName',
            //         component: () => import('@/components/org/commonInput'),
            //         type: 'inputText',
            //         isMust: true,
            //         span: 8
            //       }
            //     ],
            //     tableCols: [
            //       {
            //         prop: 'dlrCode',
            //         label: '门店编码'
            //       },
            //       {
            //         prop: 'dlrFullName',
            //         label: '门店名称'
            //       }
            //     ],
            //     formField: {
            //       dlrCode: '',
            //       dlrFullName: ''
            //     },
            //     returnField: {
            //       prop: 'dlrCode',
            //       label: '门店编码'
            //     },
            //     api: api.dlrQuery
            //   },
            //   isMust: false,
            //   span: 6
            // }
          ],
      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'controlBtn1',
              label: '操作',
              minWidth: 180,
              isComponent: true,
              comps: [
                {
                  compKey: 'propKey1',
                  labelName: '结算申请',
                  component: () => import('@/components/org/linkButton'),
                  clickEvent: index => {
                    // debugger
                    this.$router.push({ name: 'maintenanceSettledit',
                      params: { row: this.$utils.deepCopy(this.$refs['multipleTable'].list[index]) }}
                    )
                  }
                }
              ]
            },
            {
              prop: 'repairOrderCode',
              label: '派工单号'
            },
            {
              prop: 'carLicense',
              label: '车牌号'
            },
            {
              prop: 'custName',
              label: '车主名称'
            },
            {
              prop: 'custTel',
              label: '车主电话'
            },
            {
              prop: 'repairTime',
              label: '到店时间'
            },
            {
              prop: 'repairMan',
              label: '送修人'
            },
            {
              prop: 'repairTel',
              label: '送修人电话'
            },
            {
              prop: 'saName',
              label: '服务顾问'
            },
            {
              prop: 'dlrCode',
              label: '门店编码'
            },
            {
              prop: 'vin',
              label: 'VIN码'
            },
            {
              prop: 'preGetDate',
              label: '预计交车时间'
            }
          ],
      formField: {
        repairOrderCode: '',//派工单号
        carLicense: '',//车牌号
        vin: '',//vin码
        repairMan: '',//送修人
        repairTel: '',//送修人电话
        custName: '',//车主姓名
        custTel: '',//车主电话
        empCode: '',//服务顾问
        },
      // 是否使用刷新Key的方式刷新弹窗
      resetDialogKey: false,
      // 单表格模版页面组件回调事件
      callBackObj: {

      }
    }
  },
  watch: {
    '$route.params': function(val) {
      if (val.isOver) {
        this.queryTable()
      }
    }
  },
  created() {
    this.genFormField()
  },
  mounted() {
    // this.queryTable(1)
    // 新增
  },
  methods: {
    exportExcel() {
      const fileName = this.$utils.fileNameWithTime('维修结算.xlsx')
      this.$requestAPI({
        apiUrl: api.exportDetailsUsingPOST_1.APIUrl,
        // variables: { dataType: 'excel' , ...this.formField}
        variables: { dataType: 'excel' , ...this.formField}
      }).then(res => {
        this.$utils.downloadFile(res, fileName)
      }).catch(err => {
        console.log(err)
      })
    },
    genFormField() {
      const viewPageCode = this.$route.path
      this.genCodeFiled = this.genUtil(this.tableComponents)
      const formField = this.genCodeFiled()
      this.formField = this.$utils.getFormField(this, formField, this.$ConfigCache.CacheConfig.cacheData[viewPageCode])
    },
    // 生成formField
    genUtil: (array) => (attr1 = 'codeField', attr2 = 'textField') => array.reduce((p, c) => {
      if (c[attr1].includes(',')) {
        const d = c[attr1].split(',')
        for (let i = 0; i < d.length; i++) {
          p[d[i]] = ''
        }
      } else {
        p[c[attr1]] = ''
      }
      // eslint-disable-next-line no-prototype-builtins
      c.hasOwnProperty(attr2) ? p[c[attr2]] = '' : ''
      return p
    }, {})
  }
}
</script>
